﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using HGZP.BLL;
using HGZP.Model;
using HGZP.PLL;
namespace SZCB.admin.FoodManager
{
    public partial class AddFood : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {

                BindFoodClass();
                BindEnterprise();

                if (Request.QueryString["Option"] == "Edit")
                {
                    InitSourceData();
                }

            }
        
        }

        protected void InitSourceData()
        {
            string   sGuid = Request.QueryString["Id"];
            if (null == sGuid)
                return;
            CFoodBLL myCFoodBLL = new CFoodBLL();
            CFood    myCFood = new CFood();
            myCFood = myCFoodBLL.F06_SelectOneEntityByGuid(sGuid);
            if (myCFood == null)
                return;
            txtName.Text = myCFood.sFoodName;
            //ddlContentClass.Items.FindByText(myCFood.sLargeClassName).Selected = true;
            /*ddlContentClass.SelectedItem.Text = myCFood.sLargeClassName; 
            myCFood.sLargeClass = ddlContentClass.SelectedValue;
            myCFood.sEnterpriseName = ddEnterpriseList.SelectedItem.Text;
            myCFood.sEnterpriseGuid = ddEnterpriseList.SelectedValue;*/
            txtPrice.Text = myCFood.dPrice.ToString();
            txtSpecification.Text = myCFood.sSpecification;
            txtUnit.Text = myCFood.sUnit;
            /*myCFood.sFoodProductGuid = DropDownList2.SelectedValue;
            myCFood.sSubClassGuid = DropDownList1.SelectedValue;*/
            StoreTxt.Text = myCFood.sStore;
            QualityTxt.Text = myCFood.sQuality;
            ProductTxt.Text = myCFood.sBrief;
            PeiLiaoTxt.Text = myCFood.sPeiLiao;
/*            myCFood.sSubClassName = DropDownList1.SelectedItem.Text;
            myCFood.sFoodProductName = DropDownList2.SelectedItem.Text;*/
            RenZhengTxt.Text = myCFood.sRen_Zheng;
            ProductCodeTxt.Text = myCFood.sShi_pin_biao_zhun;
            ProductTypetxt.Text = myCFood.sProductType;
            FiledTxt.Text = myCFood.sMakeAddress;
            idCoverPhoto.ImageUrl = myCFood.sPhoto1;
            idCoverPhoto1.ImageUrl = myCFood.sPhoto2;
            idCoverPhoto3.ImageUrl = myCFood.sPhoto3;

/*            int i;
            for (i = 0; i < ddlContentClass.Items.Count; i++)
            {
                if (ddlContentClass.Items[i].Text == myCFood.sLargeClassName)
                {
                    ddlContentClass.Items[i].Selected = true;
                    break;
                }
            }*/

            
        }

        protected void UpdateFood()
        {
            string sGuid = Request.QueryString["Id"];
            if (null == sGuid)
                return;
            CFoodBLL myCFoodBLL = new CFoodBLL();
            CFood myCFood = myCFoodBLL.F06_SelectOneEntityByGuid(sGuid);
            if (myCFood == null)
                return;
            myCFood.sFoodName = txtName.Text;
            myCFood.dCreateDate = DateTime.Now;
            myCFood.sLargeClassName = ddlContentClass.SelectedItem.Text;
            myCFood.sLargeClass = ddlContentClass.SelectedValue;
            myCFood.sEnterpriseName = ddEnterpriseList.SelectedItem.Text;
            myCFood.sEnterpriseGuid = ddEnterpriseList.SelectedValue;
            myCFood.dPrice = Convert.ToDecimal(txtPrice.Text);
            myCFood.sSpecification = txtSpecification.Text;
            myCFood.sUnit = txtUnit.Text;
            myCFood.sFoodProductGuid = DropDownList2.SelectedValue;
            myCFood.sSubClassGuid = DropDownList1.SelectedValue;
            myCFood.sStore = StoreTxt.Text;
            myCFood.sQuality = QualityTxt.Text;
            myCFood.sBrief = ProductTxt.Text;
            myCFood.sPeiLiao = PeiLiaoTxt.Text;
            myCFood.sSubClassName = DropDownList1.SelectedItem.Text;
            myCFood.sFoodProductName = DropDownList2.SelectedItem.Text;
            myCFood.sRen_Zheng = RenZhengTxt.Text;
            myCFood.sShi_pin_biao_zhun = ProductCodeTxt.Text;
            myCFood.sProductType = ProductTypetxt.Text;
            myCFood.sMakeAddress = FiledTxt.Text;
            myCFood.sPhoto1 = idCoverPhoto.ImageUrl;
            myCFood.sPhoto2 = idCoverPhoto1.ImageUrl;
            myCFood.sPhoto3 = idCoverPhoto3.ImageUrl;


            if (!myCFoodBLL.F_Update(myCFood))
            {
                string strErrmessage = "食品更新失败";
                Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('" + strErrmessage + "');</script>");
                return;
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["Option"] == "Edit")
            {
                UpdateFood();
                return;
            }

            CFoodBLL myCFoodBLL = new CFoodBLL();
            DataTable dt = myCFoodBLL.F02_SelectHaveWhere("sFoodName = '" + txtName.Text + "'");
            if (dt.Rows.Count > 0)
            {
                string strErrmessage = "该食品已添加";
                Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('" + strErrmessage + "');</script>");
                return;
            }
            if (ddlContentClass.SelectedItem.Text == "----请选择分类----")
            {
                string strErrmessage = "请选择食品大类";
                Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('" + strErrmessage + "');</script>");
                return;

            }

            if (ddEnterpriseList.SelectedItem.Text == "----请选择企业或者输入企业名称----")
            {
                string strErrmessage = "请选择或输入企业名称";
                Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('" + strErrmessage + "');</script>");
                return;

            }

            CFood myCFood = new CFood();
            myCFood.sGuid = MyFunction.GetNewGuid();
            myCFood.sFoodName = txtName.Text;
            myCFood.dCreateDate = DateTime.Now;
            myCFood.sLargeClassName = ddlContentClass.SelectedItem.Text;
            myCFood.sLargeClass = ddlContentClass.SelectedValue;
            myCFood.sEnterpriseName = ddEnterpriseList.SelectedItem.Text;
            myCFood.sEnterpriseGuid = ddEnterpriseList.SelectedValue;
            myCFood.dPrice = Convert.ToDecimal(txtPrice.Text);
            myCFood.sSpecification = txtSpecification.Text;
            myCFood.sUnit = txtUnit.Text;
            myCFood.sFoodProductGuid = DropDownList2.SelectedValue;
            myCFood.sSubClassGuid = DropDownList1.SelectedValue;
            myCFood.sStore = StoreTxt.Text;
            myCFood.sQuality = QualityTxt.Text;
            myCFood.sBrief = ProductTxt.Text;
            myCFood.sPeiLiao = PeiLiaoTxt.Text;
            myCFood.sSubClassName = DropDownList1.SelectedItem.Text;
            myCFood.sFoodProductName = DropDownList2.SelectedItem.Text;
            myCFood.sRen_Zheng = RenZhengTxt.Text;
            myCFood.sShi_pin_biao_zhun = ProductCodeTxt.Text;
            myCFood.sProductType = ProductTypetxt.Text;
            myCFood.sMakeAddress = FiledTxt.Text;
            myCFood.sPhoto1 = idCoverPhoto.ImageUrl;
            myCFood.sPhoto2 = idCoverPhoto1.ImageUrl;
            myCFood.sPhoto3 = idCoverPhoto3.ImageUrl;
            if (!myCFoodBLL.F_Insert(myCFood))
            {
                string strErrmessage = "添加食品失败";
                Page.ClientScript.RegisterStartupScript(GetType(), "", "<script>alert('" + strErrmessage + "');</script>");
                return;
            }

        }

        private void BindFoodClass()
        {
            CEnterpriseBLL myCEnterpriseBLL = new CEnterpriseBLL();
            ddEnterpriseList.DataSource = myCEnterpriseBLL.F02_SelectHaveWhere("");
            ddEnterpriseList.DataTextField = myCEnterpriseBLL.sName;
            ddEnterpriseList.DataValueField = myCEnterpriseBLL.sGuid;
            ddEnterpriseList.DataBind();
            ListItem listItem = new ListItem();
            listItem.Text = "----请选择企业名称----";
            listItem.Value = "0";
            ddEnterpriseList.Items.Add(listItem);
            ddEnterpriseList.SelectedValue = "0";


        
        }

        private void BindEnterprise()
        {
            CsFoodClassBLL myCsFoodClassBLL = new CsFoodClassBLL();
            ddlContentClass.DataSource = myCsFoodClassBLL.F02_SelectHaveWhere("");
            ddlContentClass.DataTextField = myCsFoodClassBLL.sName;
            ddlContentClass.DataValueField = myCsFoodClassBLL.sGuid;
            ddlContentClass.DataBind();
            ListItem listItem = new ListItem();
            listItem.Text = "----请选择分类----";
            listItem.Value = "0";
            ddlContentClass.Items.Add(listItem);
            ddlContentClass.SelectedValue = "0";
        }

        
        protected void ddlContentClass_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlContentClass.SelectedValue == "0")
                return;
            CEnterpriseBLL myCEnterpriseBLL = new CEnterpriseBLL();
            ddEnterpriseList.DataSource = myCEnterpriseBLL.F02_SelectHaveWhere("sLargeClassGuid='" + ddlContentClass.SelectedValue + "'");
            ddEnterpriseList.DataTextField = myCEnterpriseBLL.sName;
            ddEnterpriseList.DataValueField = myCEnterpriseBLL.sGuid;
            ddEnterpriseList.DataBind();
            ListItem listItem = new ListItem();
            listItem.Text = "----请选择企业名称----";
            listItem.Value = "0";
            ddEnterpriseList.Items.Add(listItem);
            ddEnterpriseList.SelectedValue = "0";

            CFoodSubClassBLL myCFoodSubClassBLL = new CFoodSubClassBLL();
            DropDownList1.DataSource = myCFoodSubClassBLL.F02_SelectHaveWhere("sClassGuid='" + ddlContentClass.SelectedValue + "'");
            DropDownList1.DataTextField = myCFoodSubClassBLL.sName;
            DropDownList1.DataValueField = myCFoodSubClassBLL.sGuid;
            DropDownList1.DataBind();
            listItem = new ListItem();
            listItem.Text = "----请选择小类名称----";
            listItem.Value = "0";
            DropDownList1.Items.Add(listItem);
            DropDownList1.SelectedValue = "0";


        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (DropDownList1.SelectedValue == "0")
                return;

            CFoodProductBLL myCProductClassBLL = new CFoodProductBLL();
            DropDownList2.DataSource = myCProductClassBLL.F02_SelectHaveWhere("sSubClassGuid='" + DropDownList1.SelectedValue + "'");
            DropDownList2.DataTextField = myCProductClassBLL.sName;
            DropDownList2.DataValueField = myCProductClassBLL.sGuid;
            DropDownList2.DataBind();
            ListItem listItem = new ListItem();
            listItem.Text = "----请选择产品单元----";
            listItem.Value = "0";
            DropDownList2.Items.Add(listItem);
            DropDownList2.SelectedValue = "0";

        }
        protected void ButtonImage1_Click(object sender, EventArgs e)
        {
            bool fileOK = false;
            string fileExtension;
            string path = Server.MapPath("/Product/Image/");
            if (FileUpload1.HasFile)
            {
                fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                String[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg" };
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK = true;
                    }
                }

                try
                {
                    string strUserId = MyFunction.GetNewGuid();
                    FileUpload1.SaveAs(path + strUserId + fileExtension);
                    //idCoverPhoto.ImageUrl = MyFunction.GetHotURL(Request) + "/Product/Image/" + strUserId + fileExtension;
                    idCoverPhoto.ImageUrl =  "/Product/Image/" + strUserId + fileExtension;

                }
                catch (Exception ex)
                {
                    ;
                }

            }

        }


        protected void ButtonImage2_Click(object sender, EventArgs e)
        {
            bool fileOK = false;
            string fileExtension;
            string path = Server.MapPath("/Product/Image/");
            if (FileUpload2.HasFile)
            {
                fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                String[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg" };
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK = true;
                    }
                }

                try
                {
                    string strUserId = MyFunction.GetNewGuid();
                    FileUpload2.SaveAs(path + strUserId + fileExtension);
                    //idCoverPhoto.ImageUrl = MyFunction.GetHotURL(Request) + "/Product/Image/" + strUserId + fileExtension;
                    idCoverPhoto1.ImageUrl = "/Product/Image/" + strUserId + fileExtension;

                }
                catch (Exception ex)
                {
                    ;
                }

            }

        }


        protected void ButtonImage3_Click(object sender, EventArgs e)
        {
            bool fileOK = false;
            string fileExtension;
            string path = Server.MapPath("/Product/Image/");
            if (FileUpload3.HasFile)
            {
                fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                String[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg" };
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK = true;
                    }
                }

                try
                {
                    string strUserId = MyFunction.GetNewGuid();
                    FileUpload2.SaveAs(path + strUserId + fileExtension);
                    //idCoverPhoto.ImageUrl = MyFunction.GetHotURL(Request) + "/Product/Image/" + strUserId + fileExtension;
                    idCoverPhoto3.ImageUrl = "/Product/Image/" + strUserId + fileExtension;

                }
                catch (Exception ex)
                {
                    ;
                }

            }

        }


        protected void ButtonImage4_Click(object sender, EventArgs e)
        {
            bool fileOK = false;
            string fileExtension;
            string path = Server.MapPath("/Product/Image/");
            if (FileUpload4.HasFile)
            {
                fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                String[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg" };
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK = true;
                    }
                }

                try
                {
                    string strUserId = MyFunction.GetNewGuid();
                    FileUpload4.SaveAs(path + strUserId + fileExtension);
                    //idCoverPhoto.ImageUrl = MyFunction.GetHotURL(Request) + "/Product/Image/" + strUserId + fileExtension;
                    idCoverPhoto3.ImageUrl = "/Product/Image/" + strUserId + fileExtension;

                }
                catch (Exception ex)
                {
                    ;
                }

            }

        }

        //条形码
        protected void ButtonImage5_Click(object sender, EventArgs e)
        {
            bool fileOK = false;
            string fileExtension;
            string path = Server.MapPath("/Product/BarCode/");
            if (FileUpload5.HasFile)
            {
                fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                String[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg" };
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOK = true;
                    }
                }

                try
                {
                    string strUserId = MyFunction.GetNewGuid();
                    FileUpload5.SaveAs(path + strUserId + fileExtension);
                    //idCoverPhoto.ImageUrl = MyFunction.GetHotURL(Request) + "/Product/Image/" + strUserId + fileExtension;
                    idCoverPhoto3.ImageUrl = "/Product/BarCode/" + strUserId + fileExtension;

                }
                catch (Exception ex)
                {
                    ;
                }

            }

        }


    }
}